package vesion2.tool;



import java.awt.*;
import java.util.ArrayList;
import vesion2.object.Rectangle;

import javax.swing.*;

/**
 * 控制爆炸动画
 */
public class Bomb extends Rectangle {
    private ArrayList<Image> images;
    private int imageNum;
    private int currentImage;
    private Image cImage=null;
    private int interval;
    private Timer1 timer1;

    /**
     *
     * @param images  爆炸过程图片集
     * @param rectangle  图片范围
     * @param interval 图片刷新频率
     */
    public Bomb(ArrayList<Image> images,Rectangle rectangle,int interval){
        super(rectangle);
        this.images=images;
        if(interval>0)
            this.interval=interval;
        else
            this.interval=300;
        if(images!=null)
           imageNum=images.size();
        if(imageNum>0)
            currentImage=0;
        else
            currentImage=-1;
        timer1=new Timer1(interval);
    }

    public Bomb(Rectangle rectangle){
        this(LoadImages.getBombImages(), rectangle, 150);
    }

    public void setImages(ArrayList<Image> images) {
        this.images = images;
    }

    public void setInterval(int interval) {
        this.interval = interval;
    }

    public Image getCurrentImage(){
        if(currentImage==-1) {
            isAlive=false;
            return null;
        }
        if(timer1.isOverOneAndStar()&&currentImage<imageNum)
        {
            cImage=images.get(currentImage);
            ++currentImage;
        }
        if(currentImage==imageNum) {
           setAlive(false);
            cImage = null;
        }
        return cImage;
    }

    @Override
    public void setAlive(boolean alive) {
        super.setAlive(alive);
        timer1.End();
    }

    public void draw(Graphics g, JPanel panel){
        if (getCurrentImage() != null)
            g.drawImage(getCurrentImage(),getLeft_up().getX(),
                   getLeft_up().getY(),getWidth(), getHeight(), panel);
    }
}
